package com.ucollege.threeabnradio;


import android.app.Activity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class threeabnradioActivity extends Activity {
	Button playButton;
	private ProgressBar progressSpinner;
	
	Messenger mService = null;
    boolean mIsBound;
    final Messenger mMessenger = new Messenger(new IncomingHandler());
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        startService(new Intent(threeabnradioActivity.this, RadioService.class));
        doBindService();
        
        playButton = (Button) findViewById(R.id.playButton);
        progressSpinner = (ProgressBar) findViewById(R.id.progressBar1);
        progressSpinner.setVisibility(View.INVISIBLE);
    }
    
    // Handles messages sent from the service
    class IncomingHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case RadioService.MSG_Playing:
            	playing();
                break;
            case RadioService.MSG_Buffering:
            	buffering();
                break;
            case RadioService.MSG_Stopped:
            	stopped();
            	break;
            default:
                super.handleMessage(msg);
            }
        }
    }
    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceDisconnected(ComponentName className) {
            // This is called when the connection with the service has been unexpectedly disconnected - process crashed.
            mService = null;
        }

		public void onServiceConnected(ComponentName className, IBinder service) {
	          mService = new Messenger(service);
	            try {
	                Message msg = Message.obtain(null, RadioService.MSG_Register);
	                msg.replyTo = mMessenger;
	                mService.send(msg);
	            } catch (RemoteException e) {
	                
	            }			
		}
    };
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.About:
            //newGame();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
    private void playing() {
    	progressSpinner.setVisibility(View.INVISIBLE);
    	playButton.setText("Stop");
    }
    
    private void buffering() {
    	progressSpinner.setVisibility(View.VISIBLE);
    	playButton.setText("Stop");
    }
    
    private void stopped() {
    	progressSpinner.setVisibility(View.INVISIBLE);
    	playButton.setText("Play");
    }
    
    /*private void setStatus(int status) {
    	switch (status) {
        case RadioService.MSG_Playing:
        	playing();
            break;
        case RadioService.MSG_Buffering:
        	buffering();
            break;
        case RadioService.MSG_Stopped:
        	stopped();
        	break;
        default:
        }
    }*/
    
    private void sendMessageToService(int value) {
        if (mIsBound) {
            if (mService != null) {
                try {
                    Message msg = Message.obtain(null, value);
                    msg.replyTo = mMessenger;
                    mService.send(msg);
                } catch (RemoteException e) {
                }
            }
        }
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        
    }
    
    void doBindService() {
        bindService(new Intent(this, RadioService.class), mConnection, Context.BIND_AUTO_CREATE);
        mIsBound = true;
    }
    void doUnbindService() {
        if (mIsBound) {
            // If we have received the service, and hence registered with it, then now is the time to unregister.
            if (mService != null) {
                try {
                    Message msg = Message.obtain(null, RadioService.MSG_Unregister);
                    msg.replyTo = mMessenger;
                    mService.send(msg);
                } catch (RemoteException e) {
                    // There is nothing special we need to do if the service has crashed.
                }
            }
            // Detach our existing connection.
            unbindService(mConnection);
            mIsBound = false;
        }
    }
    
    //send message to radioService to start
    public void startStream(View view) {
    	sendMessageToService(RadioService.MSG_Play);
    }
}